﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using MS.Core.Base.Transaction;
using LHS.Core.TableInfo;
using System.Collections.Specialized;
using MS.Core.Constants.Objects;

namespace LHS.Win
{
    public partial class frmHoaDonYTa : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmHoaDonYTa(string _patName, string _patID, string _clrISN, string _tienThuoc, string _tienkham, string _tienThuThuat, string _tienPhauThuat, string _invISN)
        {
            this.patName = _patName;
            this.patID = _patID;
            this.clrISN = _clrISN;
            this.tbtTienKham = _tienkham;
            this.tbTienPhauThuat = _tienPhauThuat;
            this.tbtTienThuThuat = _tienThuThuat;
            this.tbtTienKham = _tienkham;
            this.tbtTienThuoc = _tienThuoc;
            this.invISN = _invISN;
            this.InitializeComponent();
        }

        private void btCapNhat_Click(object sender, EventArgs e)
        {
            if (this.txtLido.Text == "")
            {
                MessageBox.Show("Vui lòng nhập lý do cập nhật", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else if (this.check())
            {
                if (!this.txtTienThuThuat.ReadOnly && this.txtTienThuThuat.Value == 0)
                {
                    MessageBox.Show("Vui lòng nhập tiền thủ thuật lớn hơn 0!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                if (!this.txtTienPhauThuat.ReadOnly && this.txtTienPhauThuat.Value == 0)
                {
                    MessageBox.Show("Vui lòng nhập tiền phẫu thuật lớn hơn 0!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    string isn = Class.isn;
                    TransactionManager trans = TransactionManager.ThreadTransactionMgr();
                    try
                    {
                        trans.BeginTransaction();

                        //insert vao bang UserUpdatedInvoices 
                        DataTable dtUserUpdatedInvoices = InfoUserUpdatedInvoices.GetStructure();
                        DataRow drUserUpdatedInvoices = dtUserUpdatedInvoices.NewRow();
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USRISN] = isn;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.INVISN] = this.invISN;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USNDATE] = DateTime.Now;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USNREASON] = this.txtLido.Text;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USNSTATUS] = 0;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USNMONEYBEFOREUPDATE] = this.money;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.CLRISN] = this.clrISN;
                        objController.InsertRow(drUserUpdatedInvoices, new string[] { InfoUserUpdatedInvoices.USNISN });
                      
                        //update vao bang Invoice
                        ListDictionary updateField = new ListDictionary();

                        updateField.Add(InfoInvoices.INVEXAMINATIONPAYMENT, txtTienKham.Value);
                        updateField.Add(InfoInvoices.INVMEDICINEPAYMENT, txtTienThuoc.Value);
                        updateField.Add(InfoInvoices.INVMEDICINESKILLPAYMENT, this.txtTienThuThuat.Value );
                        updateField.Add(InfoInvoices.INVSURGERYPAYMENT, this.txtTienPhauThuat.Value);
                        updateField.Add(InfoInvoices.INVTOTALPAYMENT, this.txtTongCong.Value);
                      
                        List<ExpressionObject> param = new List<ExpressionObject>();
                        param.Add(new ExpressionObject(InfoInvoices.CLRISN, this.clrISN));
                        objController.UpdateRow(InfoInvoices.TABLENAME, updateField, param);

                        trans.CommitTransaction();
                        MessageBox.Show("Hóa đơn đã được cập nhật", "Cập nhật thành công", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    catch (Exception ex)
                    {
                        trans.RollbackTransaction();
                        Class.ShowException(ex);
                    }
                }
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private bool check()
        {
            if (this.txtTienKham.ReadOnly == false && this.txtTienKham.Value == 0)
            {
                MessageBox.Show("Vui lòng nhập tiền khám lớn hơn 0!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            if (this.txtTienThuoc.ReadOnly == false && this.txtTienThuoc.Value == 0)
            {
                MessageBox.Show("Vui lòng nhập tiền thuốc lớn hơn 0!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
         
            return true;
        }

        private void frmHoaDon_Load(object sender, EventArgs e)
        {
            this.lbID.Text = this.patID;
            this.lbBenhNhan.Text = this.patName;

            this.txtTienPhauThuat.Value = decimal.Parse(this.tbTienPhauThuat == string.Empty? "0": this.tbTienPhauThuat);
            this.txtTienKham.Value = decimal.Parse(this.tbtTienKham == string.Empty ? "0" : this.tbtTienKham);
            this.txtTienThuoc.Value = decimal.Parse(this.tbtTienThuoc == string.Empty ? "0" : this.tbtTienThuoc);
            this.txtTienThuThuat.Value = decimal.Parse(this.tbtTienThuThuat == string.Empty ? "0" : this.tbtTienThuThuat);
            this.txtTongCong.Value = txtTienPhauThuat.Value + txtTienKham.Value + txtTienThuoc.Value + txtTienThuThuat.Value;
            this.money = this.txtTongCong.Value.ToString();
            try
            {
                ArrayList arrColumn = new ArrayList();
                arrColumn.Add(InfoClinicalRecord.CLRSKILL);
                arrColumn.Add(InfoClinicalRecord.CLRSURGERY);
                arrColumn.Add("HasPrescription");

                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoClinicalRecord.CLRISN, this.clrISN));
                DataTable dt= objController.GetData("vw_CalculateInvoices", false, arrColumn, param);
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (dt.Rows[0][0].ToString() == string.Empty)
                    {
                        this.txtTienThuThuat.Value = 0;
                        this.txtTienThuThuat.ReadOnly = true;
                    }
                    if (dt.Rows[0][1].ToString() == string.Empty)
                    {
                        this.txtTienPhauThuat.Value = 0;
                        this.txtTienPhauThuat.ReadOnly = true;
                    }
                    if (dt.Rows[0][2].ToString() == "0")
                    {
                        this.txtTienThuoc.Value = 0;
                        this.txtTienThuoc.ReadOnly = true;
                    }
                }
            }
            catch (Exception ex)
            {
                this.txtTienThuThuat.Value = 0;
                this.txtTienThuThuat.ReadOnly = true;
                this.txtTienPhauThuat.Value = 0;
                this.txtTienPhauThuat.ReadOnly = true;
                this.txtTienThuoc.Value = 0;
                this.txtTienThuoc.ReadOnly = true;
                Class.ShowException(ex);
            }
        }
    
        /// <summary>
        /// tinh lai tong cong khi leave bat cu hang muc tinh tien nao
        /// </summary>
        private void tinhtongcong()
        {
            this.txtTongCong.Value = txtTienPhauThuat.Value + txtTienKham.Value + txtTienThuoc.Value + txtTienThuThuat.Value;
        }

        private void txtTienKham_Leave(object sender, EventArgs e)
        {
            tinhtongcong();
        }
    }
}